package net.htjs.mobile.qxoa.gw;

import net.htjs.mobile.qxoa.R;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

public class NActivity extends Activity implements OnClickListener {

	private Button setNotificationSoundBtn = null;
	private Button showNotificatioBtn = null;
	private Button cancelNotificationBtn = null;
	private Intent mIntent = null;
	private PendingIntent mPendingIntent = null;
	private Notification mNotification = null;
	private NotificationManager mNotificationManager = null;

	private static final int NOTIFICATION_STATE = 1;
	private static final int RINGTONE_PICKED = 2;

	private Uri notifiSounds = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.notification);
		mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

		setNotificationSoundBtn = (Button) findViewById(R.id.button0);
		setNotificationSoundBtn.setOnClickListener(this);
		showNotificatioBtn = (Button) findViewById(R.id.button1);
		showNotificatioBtn.setOnClickListener(this);
		cancelNotificationBtn = (Button) findViewById(R.id.button2);
		cancelNotificationBtn.setOnClickListener(this);

		mIntent = new Intent(this, ToNotificationActivity.class);
		mPendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);

		mNotification = new Notification();
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.button0:
			SharedPreferences sharedPreferences = PreferenceManager
					.getDefaultSharedPreferences(this);
			Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
			// Allow user to pick 'Default'
			intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
			intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI,
					RingtoneManager
							.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
			// Show only ringtones
			intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
					RingtoneManager.TYPE_NOTIFICATION);
			// Don't show 'Silent'
			intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
			String notifi_sound = sharedPreferences.getString(
					"notification_sounds", null);
			if (notifi_sound != null) {
				intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,
						Uri.parse(notifi_sound));
			}
			// Launch!
			startActivityForResult(intent, RINGTONE_PICKED);
			break;
		case R.id.button1:
			mNotification.icon = R.drawable.logonyoa;
			mNotification.number = 10;
			mNotification.tickerText = "大明ZeroSon Notification";
			mNotification.sound = notifiSounds;
			mNotification.defaults = Notification.DEFAULT_VIBRATE
					| Notification.DEFAULT_LIGHTS;
			mNotification.flags = Notification.FLAG_INSISTENT;
			mNotification.setLatestEventInfo(this, "大明Notification",
					"This is Daming`s Notification Test!", mPendingIntent);
			mNotificationManager.notify(NOTIFICATION_STATE, mNotification);
			break;
		case R.id.button2:
			mNotificationManager.cancel(NOTIFICATION_STATE);
			break;
		default:
			break;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		SharedPreferences sharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(this);
		SharedPreferences.Editor editor = sharedPreferences.edit();

		if (resultCode != RESULT_OK) {
			return;
		}
		switch (requestCode) {
		case RINGTONE_PICKED: {
			notifiSounds = data
					.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
			editor.putString("notification_sounds", notifiSounds.toString());
			editor.commit();
			break;
		}
		default:
			break;
		}
	}
}